package com.dongan.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongan.pojo.Cell;
import com.dongan.pojo.Codes;
import com.dongan.service.CellService;
import com.dongan.service.CodesService;
import com.dongan.utils.CommonProp;
import com.dongan.utils.NumberFormatUtil;
import com.dongan.utils.ResultResponseUtil;
import com.dongan.utils.SerialNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Action;

/**
 * 安全库存单元
 * @author longTao
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/cell")
@CrossOrigin
public class CellController {
    @Autowired
    private CellService service;
    @Autowired
    private CodesService codesService;
    /**
     * 查询产品
     * @param checkTag 审核编号
     * @param firstId 一级分类查询
     * @param secondId 二级分类查询
     * @param threeId 三级分类查询
     * @param pageNum  页码
     * @param pageSize 页码大小

     * @return
     */
    @GetMapping(value = "/queryProduct")
    public ResultResponseUtil queryProduct(String checkTag,String firstId,String secondId,String threeId,String pageNum,String pageSize){
        QueryWrapper<Cell> queryWrapper=new QueryWrapper<>();
        Page<Cell> filePage=new Page<>();
        if (!StringUtils.isEmpty(firstId)){
            queryWrapper.eq("FIRST_KIND_ID",Long.valueOf(firstId));
        }
        if (!StringUtils.isEmpty(checkTag)){
            queryWrapper.eq("CHECK_TAG",checkTag);
        }
        if (!StringUtils.isEmpty(secondId)){
            queryWrapper.eq("SECOND_KIND_ID",Long.valueOf(secondId));
        }
        if (!StringUtils.isEmpty(threeId)){
            queryWrapper.eq("THIRD_KIND_ID",Long.valueOf(threeId));
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum="1";
        }
        if (StringUtils.isEmpty(pageSize)){
            pageSize="2";
        }
        if (!StringUtils.isEmpty(checkTag)){
            queryWrapper.eq("CHECK_TAG",checkTag);
        }
        filePage.setSize(Long.valueOf(pageSize));
        filePage.setCurrent(Long.valueOf(pageNum));
        Page<Cell> page = service.page(filePage, queryWrapper);
        return new ResultResponseUtil(page);
    }

    /**
     * 根据库存编号查询
     * @param id
     * @return
     */
    @GetMapping("/queryByCellId")
    public Object queryByCellId(String id){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("STORE_ID",id);
        Cell c=service.getOne(wrapper);
        return new ResultResponseUtil(true,"查询成功!",c);
    }
    /**
     * 添加库存单元
     * @param cell
     * @return
     */
    @PostMapping("addCell")
    public Object addCell(@RequestBody Cell cell){
        //库存编号	400+当前日期+4位流水号
        String id=400+ NumberFormatUtil.numberFormat2(4, SerialNumber.cellSN);
        //审核标记
        cell.setCheckTag("S001-0");
        boolean flag=service.save(cell);
        if(flag){
            SerialNumber.cellSN=SerialNumber.cellSN+1;
            Codes codes=new Codes();
            codes.setId(6L);
            codes.setDefValue(Integer.valueOf(SerialNumber.cellSN).toString());
            codesService.updateById(codes);
        }
        return new ResultResponseUtil(flag,"添加完成!");
    }
    /**
     *  添加库存单元
     * @param cell
     * @return
             */
    @PostMapping("checkCell")
    public Object checkCell(@RequestBody Cell cell){
        //CHECKER	VARCHAR2(50)	复核人
        //CHECK_TIME	DATE	复核时间
        //审核标记 前端赋值
        boolean flag=service.updateById(cell);

        return new ResultResponseUtil(flag,"审核完成!");
    }
    /**
     * 添加库存单元
     * @param cell
     * @return
     */
    @PostMapping("updateCell")
    public Object updateCell(@RequestBody Cell cell){

        //审核标记
        cell.setCheckTag("S001-0");
        boolean flag=service.updateById(cell);

        return new ResultResponseUtil(flag,"修改完成!");
    }
}

